/*
 *  SimpleSDL.cpp
 *  SimpleSDL
 *
 *  Created by david on 19/06/2010.
 */


#define SCREEN_WIDTH	800
#define SCREEN_HEIGHT	600
#define SCREEN_BPP		32

#include <iostream>
#include <vector>

#ifdef __APPLE__//Apple Headers
//#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#include "SDL.h"
#include "SDL_opengl.h"
	#else//Windows and linux headers
#include "SDL\SDL.h"
#include "SDL\SDL_opengl.h"
#include <gl\gl.h>			
#include <gl\glu.h>		
#include "glut.h"
#endif

//Surface for OpenGL to be drawn to
SDL_Surface *screen = NULL;

//False unti program quit by ESC or clicking top corner 'X'
bool quit;

//Keyboard and other Input Device events
SDL_Event event;


//Methods In Program
bool init();
void init_GL();
bool init_SDL();
void clean_up();
void display();
void draw();
void handle_input();

/*--------------------------------------------------
 *	 Setup SDL
 *--------------------------------------------------*/
bool init_SDL()
{
	//Init all SDL Parts
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		return false;
	}
	
	//screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL | SDL_DOUBLEBUF | SDL_FULLSCREEN );	//FULLSCREEN
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL | SDL_DOUBLEBUF);
	SDL_WM_SetCaption("Simple SDL Demo", NULL);
	
	
	return true;
}

/*--------------------------------------------------
 *	 Init OpenGL
 *--------------------------------------------------*/
void init_GL()
{	
	glClearColor(0.8, 0.7, 0.9, 1.0);
	glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}


/*--------------------------------------------------
 *	 Init Everything
 *--------------------------------------------------*/
bool init()
{
	if(init_SDL() == false)
	{
		std::cout << "Setting Up SDL Failed - Now Exiting or Crashing" << std::endl;
		return false;
	}
	else
		std::cout<< "SDL Initialized " << std::endl;
	
	init_GL();
	std::cout << "OpenGL Initialized" << std::endl;
	
	quit = false;
		
	return true;
}


/*-----------------------------------------------------------------
 *			Clean Up RAM Before Quitting
 *----------------------------------------------------------------- */
void clean_up()
{	
	//Close All SDL Subsystems
	SDL_Quit();
}

/*--------------------------------------------------
 *	Draw Scene Code
 *--------------------------------------------------*/
void draw()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(43.0, SCREEN_WIDTH/SCREEN_HEIGHT, 1, 50);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//DRAW CODE HERE

	SDL_GL_SwapBuffers();	
}

/*--------------------------------------------------
 *	 Handle input Code
 *--------------------------------------------------*/
void handle_input()
{
	if(SDL_PollEvent(&event))
	{				
		switch(event.type)
		{
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym)
			{
					//ESCAPE
				case SDLK_ESCAPE:
					quit = true;
					break;
					//SPACE
				default:
					break;
			}
				break;
				//EXIT SYSTEM
			case SDL_QUIT:
				quit = true;
				break;
		}
	}
}


/*-----------------------------------------------------------------
 *			Main Method
 *----------------------------------------------------------------- */
int main(int argc, char *args[])
{	
	if(init() == false)
	{
		std::cout << "Problem Initialising" << std::endl;
		return 1;
	}
	
	
	do{
		//GAME MAIN LOOP GOES HERE 	
		handle_input();
		draw();
		
	}while(!quit);
	
	clean_up();
	return 0;
	
	
}



